package com.alogic.xscript.plugins;

import org.apache.commons.lang3.StringUtils;

import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * 在当前文档增加一个对象
 * 
 * @author duanyy
 * 
 * @version 1.6.8.14 [20170509 duanyy] <br>
 * - 增加xscript的中间文档模型,以便支持多种报文协议 <br>
 * 
 * @version 1.6.11.31 [20170522 duanyy] <br>
 * - 增加是否新增的变量输出;
 * 
 * @version 1.6.12.4 [20171029 duanyy] <br>
 * - 支持bridge机制; <br>
 * 
 * @version 1.6.12.12 [20181207 duanyy] <br>
 * - 增加BridgeAdd和BridgeSet实现;
 *
 * @version 1.6.12.23 [20190304 duanyy] <br>
 * - 修正定位到数组对象导致空指针问题; <br>
 *     
 */
public class Obj extends Segment {
	protected String tag = "data";
	protected String id = "$obj";
	protected String $bridgeId = "";
	
	public Obj(String tag, Logiclet p) {
		super(tag, p);
	}

	@Override
	public void configure(Properties p){
		super.configure(p);
		
		id = PropertiesConstants.getString(p,"id",id,true);
		tag = PropertiesConstants.getRaw(p,"tag",tag);
		$bridgeId = PropertiesConstants.getRaw(p,"bridge","");
	}
	
	protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
		String tagValue = ctx.transform(tag);
		if (StringUtils.isNotEmpty(tagValue)){
			XsObject template = current.getObjectChild(tagValue, false);
			if (template != null){
				String bridgeId = PropertiesConstants.transform(ctx, $bridgeId, "");
				try {
					if (StringUtils.isNotEmpty(bridgeId)){
						ctx.setObject(bridgeId, template);
					}
					ctx.SetValue(id, "true");
					super.onExecute(root,template, ctx, watcher);
				}finally{
					if (StringUtils.isNotEmpty(bridgeId)){
						ctx.removeObject(bridgeId);
					}					
				}
			}else{
				template = current.getObjectChild(tagValue, true);
				if (template != null) {
					// 当文档中定位到的tag不是对象而是数组的时候，template可能为null
					String bridgeId = PropertiesConstants.transform(ctx, $bridgeId, "");
					try {
						if (StringUtils.isNotEmpty(bridgeId)) {
							ctx.setObject(bridgeId, template);
						}
						ctx.SetValue(id, "false");
						super.onExecute(root, template, ctx, watcher);
					} finally {
						if (StringUtils.isNotEmpty(bridgeId)) {
							ctx.removeObject(bridgeId);
						}
					}
				}
			}
		}
	}	
	
	/**
	 * 对象桥
	 * @author yyduan
	 *
	 */
	public static final class Bridge extends Segment {
		protected String $bridgeId = "";
		
		public Bridge(String tag, Logiclet p) {
			super(tag, p);
		}

		@Override
		public void configure(Properties p){
			super.configure(p);
			
			$bridgeId = PropertiesConstants.getRaw(p,"bridge","");
		}
		
		protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
			String bridgeId = PropertiesConstants.transform(ctx, $bridgeId, "");
			if (StringUtils.isNotEmpty(bridgeId)){
				XsObject obj = ctx.getObject(bridgeId);
				if (obj != null){
					super.onExecute(root, obj, ctx, watcher);
				}
			}
		}	
	}
	
	/**
	 * 设置对象桥
	 * @author yyduan
	 *
	 */
	public static final class BridgeAdd extends AbstractLogiclet {
		protected String $bridgeId = "";
		
		public BridgeAdd(String tag, Logiclet p) {
			super(tag, p);
		}

		@Override
		public void configure(Properties p){
			super.configure(p);			
			$bridgeId = PropertiesConstants.getRaw(p,"bridge","");
		}
		
		protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
			String bridgeId = PropertiesConstants.transform(ctx, $bridgeId, "");
			if (StringUtils.isNotEmpty(bridgeId)){
				ctx.setObject(bridgeId,current);
			}
		}	
	}
	
	/**
	 * 设置对象桥
	 * @author yyduan
	 *
	 */
	public static final class BridgeRemove extends AbstractLogiclet {
		protected String $bridgeId = "";
		
		public BridgeRemove(String tag, Logiclet p) {
			super(tag, p);
		}

		@Override
		public void configure(Properties p){
			super.configure(p);			
			$bridgeId = PropertiesConstants.getRaw(p,"bridge","");
		}
		
		protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
			String bridgeId = PropertiesConstants.transform(ctx, $bridgeId, "");
			if (StringUtils.isNotEmpty(bridgeId)){
				ctx.removeObject(bridgeId);
			}
		}	
	}	
}
